package com.ziytek.web.citizen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ziytek.web.citizen.configuration.db.DDS;
import com.ziytek.web.citizen.constant.DdsConstant;
import com.ziytek.web.citizen.pojo.basic.BoardConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author fenghx
 * @version 1.0
 * @className BoardConfigMapper
 * Description:
 * @date 2024/3/18 17:20
 */
@Mapper
public interface BoardConfigMapper extends BaseMapper<BoardConfig> {

    /**
     * 查询通道位置
     *
     * @param lineNo    第几行
     * @param channelNo 通道号
     * @return BoardConfigTable通道位置
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM board_config WHERE lineNo = #{lineNo} AND channelNo= #{channelNo} LIMIT 1")
    BoardConfig queryByLineNo(@Param("lineNo") int lineNo, @Param("channelNo") int channelNo);


    /**
     * 查询通道位置
     *
     * @param lineNo    第几行
     * @param channelNo 通道号
     * @return BoardConfigTable通道位置
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM board_config WHERE door = #{door} AND lineNo = #{lineNo} AND channelNo= #{channelNo} LIMIT 1")
    BoardConfig queryByDoorAndLineNo(@Param("door") String door, @Param("lineNo") int lineNo, @Param("channelNo") int channelNo);

    /**
     * 查询通道位置
     *
     * @param address   address
     * @param channelNo 通道号
     * @return BoardConfigTable通道位置
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM board_config WHERE address = #{address} AND channelNo= #{channelNo} LIMIT 1")
    BoardConfig queryByChannelNo(@Param("address") int address, @Param("channelNo") int channelNo);

    /**
     * 查询通道位置
     *
     * @param door      door
     * @param lineNo    行号
     * @param channelNo channelNo
     * @return BoardConfigTable通道位置
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM board_config WHERE door = #{door} AND lineNo= #{lineNo} and channelNo= #{channelNo} LIMIT 1")
    BoardConfig queryByDoorAndChannelNo(@Param("door") String door, @Param("lineNo") int lineNo, @Param("channelNo") int channelNo);

    /**
     * 根据通道号查询板子总数
     *
     * @return List<String> 板子地址总数
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT addressHex as address FROM board_config where channelNo =1 order by address ")
    List<String> queryListByChannelNo();

    /**
     * 查询所有板子
     *
     * @return List<BoardConfig> 所有板子数据
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM board_config where id is not null order by address ")
    List<BoardConfig> queryAllList();

    /**
     * 查询所有板子
     *
     * @param doorLists 门号
     * @return List<BoardConfig> 所有板子数据
     */
    @DDS(DdsConstant.DS_BASIC)
    //@Select("SELECT * FROM board_config where door in (#{doorListString}) order by address, channelNo")

    @Select("<script> " +
            "SELECT * FROM board_config where door in  " +
            "<foreach item='door' collection='doorLists' open='(' separator=',' close=')'> " +
            "#{door} " +
            "</foreach> " +
            " order by address, channelNo " +
            "</script>")
    List<BoardConfig> queryAllListByDoor(@Param("doorLists") List<String> doorLists);

    /**
     * 查询所有板子
     *
     * @param door 门号列表
     * @return List<BoardConfig> 所有板子数据
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT DISTINCT(addressHex) FROM board_config where door = #{0} order by address, channelNo")
    List<String> queryByDoorList(String door);

    /**
     * 查询所有板子
     *
     * @param door 门号列表
     * @return List<BoardConfig> 所有板子数据
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT DISTINCT(address) FROM board_config where door = #{0} order by address, channelNo")
    List<Integer> queryByDoorListNum(String door);

    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT DISTINCT(addressHex) FROM board_config where door = #{door} and channelNo = #{channelNo} and lineNo = #{lineNo} order by address, channelNo")
    List<String> queryByDoorChannelLine(String door, int channelNo, int lineNo);

    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT DISTINCT(address) FROM board_config where door = #{door} and channelNo = #{channelNo} and lineNo = #{lineNo} order by address, channelNo")
    List<Integer> queryByDoorChannelLineNum(String door, int channelNo, int lineNo);
}